<?php
// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 曾虎 [ 1427305236@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术四部 出品
// +----------------------------------------------------------------------
namespace app\api\controller\v1;

use app\api\controller\Base;
use service\ApiReturn;
use think\Db;
use app\goods\model\Goods as GoodsModel;
use app\goods\model\Brand as BrandModel;
use app\goods\model\Category;
use app\goods\model\GoodsLabelService;

/**
 * 搜索管理接口
 * Class Search
 * @package app\api\controller\v1
 */
class Search extends Base
{
   

    //搜索关键词推荐
    public function keyword_recommend($data)
    {
    
        $new_lists = Db::name('goods')->where(['is_recommend' => 1, 'is_sale' => 1, 'status' => 1, 'is_delete' => 0])->order('update_time DESC')->limit(3)->column('keywords');
      
        return ApiReturn::r(1, array_filter(array_unique($new_lists)), lang('请求成功'));
    }
    
    
    /**
     * 高级搜索栏数据返回
     * @param array $requests .keywords 搜索关键字
     * @author 曾虎 [ 1427305236@qq.com ]
     * @since 2020年12月21日15:19:36
     */
    public function getAdvancedSearch($requests = [])
    {
        // 模拟参数
        // $requests = array(
        //     'keyword' => lang('白酒'),
        //     'goods_cid' => '1',
        // );
        // 获取商品信息
        if ($requests['keyword']) {
            $where[] = GoodsModel::getKeywordWhereMap($requests['keyword'], "name|keywords");
        }
        if ($requests['goods_cid']) {
            $category_ids = Category::getChildsId($requests['goods_cid']);
            $where[] = ['cid', "in", $category_ids];
        }

        $goodsList = GoodsModel::getGoodsList($where);

        $cids = array_filter(array_unique(explode(',', $requests['cids'])));

        // 获取品牌信息
        $brandList = [];
        if (!empty($goodsList['brand_ids'])) {
            $brand_ids = array_filter(array_unique(explode(',', $requests['brand_ids'])));
            if (!empty($brand_ids)) {
                $brandList = BrandModel::field('id brand_id, name brand_name')->where("id", "in", $brand_ids)->where("status", 1)->select();
            }
        }

        // 获取匹配条件的所有分类信息
        $typeList['allType'] = [];
        if ($cids) {
            $typeList['allType'] = Category::field('id cid, name cname')->where("id", "in", $cids)->where("status", 1)->select();
        }
        if (!empty($brandList) && !empty($cids)) {
            foreach ($brandList as $val) {
                $goodCids = GoodsModel::where("cid", "in", $cids)->where("brand_id", $val["brand_id"])->column("cid");
                $typeList[$val['brand_id']] = Category::field('id cid, name cname')->where("id", "in", array_unique($goodCids))->where("status", 1)->select();
            }
        }

        // 获取商品服务标签
        $goodsServiceLabel = [];
        if (!empty($goodsList['goods_label_services'])) {
            $goods_label_services = array_filter(array_unique(explode(',', $requests['goods_label_services'])));
            if (!empty($goods_label_services)) {
                $goodsServiceLabel = GoodsLabelService::field("id label_id, name label_name")->where("id", "in", $goods_label_services)->select();
            }
        }

        // 信息返回
        $data = array(
            'goodsServiceLabel' => $goodsServiceLabel,
            'brandList' => $brandList,
            'typeList' => $typeList,
        );

        return ApiReturn::r(1, $data, lang('获取成功'));
    }


}
